﻿using SRB.Tg.DrawingStandard;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Teigha.DatabaseServices;
using Teigha.Geometry;

namespace SRB.Tg.Model
{
    public class BridgeCatalogTable
    {
        static string _dwgPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Template\\RebarGrade.dwg";
        static double rowH = 6;

      
        /// <summary>
        /// 汇总表
        /// </summary>
        /// <param name="db"></param>
        /// <param name="ext"></param>
        /// <param name="startPoint"></param>
        /// <param name="dt"></param>
        /// <param name="headName"></param>
        /// <param name="rowNum"></param>
        /// <param name="colNum"></param>
        /// <param name="tbHeight"></param>
        /// <param name="tbWidth"></param>
        /// <param name="txtStyle"></param>
        /// <param name="scale"></param>
        public static void DrawCatalogTable(Database db, ref List<Extents2d> extLst, Point3d startPoint, System.Data.DataTable dtQu, List<string> headName,
         string txtStyle, double scale, string title)
        {
            List<Tuple<System.Data.DataTable, int, int>> tuLst = new List<Tuple<System.Data.DataTable, int, int>>();
            int tbRow = 36;
            DivTable(dtQu, tbRow, out tuLst);
            int colNum = headName.Count;
            Extents2d ext = new Extents2d(startPoint.C2D(), startPoint.C2D());
            for (int n = 0; n < tuLst.Count;n++)
            {

                System.Data.DataTable dtDiv = tuLst[n].Item1;
                startPoint = startPoint.C3D(420 * scale * n);
                ext = new Extents2d(startPoint.C2D(), startPoint.C2D());
               

                System.Data.DataTable dt= dtDiv;
              
                int rowNum = dt.Rows.Count + 1;
                #region 表格绘制

                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
                    BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                        OpenMode.ForWrite) as BlockTableRecord;
                    LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                    ObjectId id;
                    TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                    id = st[txtStyle];
                    // 创建table 
                    Table tb = new Table();
                    tb.SetSize(rowNum, colNum);       // 设置几行几列
                    tb.Position = startPoint;
                    tb.GenerateLayout();

                    // 设置表头
                    for (int i = 0; i < colNum; ++i)
                    {
                        tb.Cells[0, i].TextString = headName[i]; //获取i行j列数据
                        tb.Cells[0, i].Contents[0].TextHeight = 3 * scale;
                        tb.Cells[0, i].Alignment = CellAlignment.MiddleCenter;
                        tb.Cells[0, i].Contents[0].Rotation = 0;
                        tb.Cells[0, i].Borders.Horizontal.Margin = 2 * scale;
                        tb.Cells[0, i].Contents[0].TextStyleId = id;
                    }
                    tb.Rows[0].Height = 8 * scale;
                    string value = " ";
                    int changNum = colNum - 5;
                    if (dt.Rows.Count > 0)
                    {
                        for (int m = 1; m < rowNum; m++)
                        {
                            System.Data.DataRow dr = dt.Rows[m - 1];
                            for (int i = 0; i < colNum; ++i)
                            {
                                double weight = 0;
                                switch (i)
                                {
                                    case 0:
                                        value = dr["no"].ToString();
                                        break;
                                    case 1:
                                        value = dr["name"].ToString();
                                        break;
                                    case 2:
                                        value = dr["drawing_no"].ToString().Replace("/", "  ");
                                        break;
                                    case 3:
                                        value = dr["pages"].ToString();
                                        break;
                                    case 4:
                                        value = dr["remark"].ToString();
                                        break;                                   
                                }
                                if (!string.IsNullOrWhiteSpace(value))
                                    tb.Cells[m, i].TextString = value; //获取i行j列数据
                                else
                                    tb.Cells[m, i].TextString = " "; //获取i行j列数据
                                tb.Cells[m, i].Contents[0].TextHeight = 2.5 * scale;
                                tb.Cells[m, i].Alignment = CellAlignment.MiddleCenter;
                                tb.Cells[m, i].Contents[0].Rotation = 0;
                                tb.Cells[m, i].Borders.Horizontal.Margin = 1 * scale;
                                tb.Cells[m, i].Contents[0].TextStyleId = id;
                            }
                        }
                    }

                    // 设置列宽,行高
                    for (int i = 0; i < colNum; ++i)
                    {
                        if (i == 0)
                        {
                            tb.Columns[i].Width = 15 * scale;
                        }
                        else if (i == 1)
                        {
                            tb.Columns[i].Width = 180 * scale;
                        }
                        else if (i ==2|| i == 4)
                        {
                            tb.Columns[i].Width = 60 * scale;
                        }                   
                        else
                        {
                            tb.Columns[i].Width = 10 * scale;
                        }
                    }

                    for (int i = 1; i < rowNum; ++i)
                    {
                        tb.Rows[i].Height = 6 * scale;
                    }
              
                    tb.LayerId = lt["H细线"];
                    modelSpace.AppendEntity(tb);
                    tr.AddNewlyCreatedDBObject(tb, true);
                    ext = ext.Add(new Extents2d(tb.Bounds.Value.MinPoint.C2D(), tb.Bounds.Value.MaxPoint.C2D()));

                    List<Point2d> pList = new List<Point2d>();
                    Point2d pt1 = tb.Position.C2D();
                    pList.Add(pt1);
                    Point2d pt2 = pt1.C2D(0, -tb.Height);
                    pList.Add(pt2);
                    Point2d pt3 = pt1.C2D(tb.Width, -tb.Height);
                    pList.Add(pt3);
                    Point2d pt4 = pt1.C2D(tb.Width, 0);
                    pList.Add(pt4);
                    PLPloter.AddPolylineByPointList(db, pList, ref ext, "H粗线", true);
                    if (n == 0)
                        TextPloter.AddTitle(db, tb.Position.C2D(tb.Width * 0.5, 1), ref ext, title, scale);
                    else
                        TextPloter.AddTitle(db, tb.Position.C2D(tb.Width * 0.5, 1), ref ext, title + "(续" + n + ")", scale);
                    tr.Commit();

                }
                #endregion
     
                extLst.Add(ext);
            }
        }

        private static void DivTable(System.Data.DataTable dt,int tbSize, out List<Tuple<System.Data.DataTable, int, int>> tuLst)
        {
            tuLst =new List<Tuple<System.Data.DataTable, int, int>>();
            if (dt.Rows.Count > 0)
            {
                System.Data.DataTable dtDiv = dt.Clone();
                int num = dt.Rows.Count / tbSize;
                int tableNum = num * tbSize >= dt.Rows.Count ? num : num + 1;
                int number = 0;
                for (int m = 0; m < tableNum; m++)
                {
                    number = 0;
                    dtDiv = dt.Clone();
                    for (int i = 0; i < tbSize; i++)
                    {
                        if (m * tbSize + i == dt.Rows.Count - 1)
                        {
                            dtDiv.Rows.Add(dt.Rows[m * tbSize + i].ItemArray);
                            number++;
                            break;
                        }
                        else
                        {
                            dtDiv.Rows.Add(dt.Rows[m * tbSize + i].ItemArray);
                            number++;
                        }
                    }
                    Tuple<System.Data.DataTable, int, int> tu = new Tuple<System.Data.DataTable, int, int>(dtDiv, number, m);
                    tuLst.Add(tu);
                }
                //foreach (Tuple<eDB, int, double> item in newLst)
                //{
                //    totalLength += item.Item3 * 0.01 * item.Item2;
                //    totalWeight += weight * item.Item3 * 0.01 * item.Item2;
                //}
            }
        }

  
 
    }
}
